# -*- coding: utf-8 -*-

from django.db import models, transaction, connection
# Create your models here.

GENDER_CHOICES = (
	('m', 'male'),
	('f', 'female'),
)

MARITAL_CHOICES = (
	('m', 'married'),
	('s', 'single'),
	('d', 'divorced'),
)

OTA_RX_CHOICES = (
	(1, 'OTA'),
	(0, 'Rx'),
)

UNITS = (
	('mg', 'mg'),
	('g', 'g'),
	('kg', 'kg'),
	('pill', 'pill'),
	('box', 'box'),
)

class Department(models.Model):
	""" Department Information """
	dept_id = models.AutoField(primary_key=True)
	dept_name = models.CharField(max_length = 20, db_index = True)
	dept_location = models.CharField(max_length = 20)
	intro = models.TextField()
	class Meta:
		db_table = 'department'
	def __unicode__(self):
		return unicode(self.dept_name)

class Patient_Info(models.Model):
	""" Personal Information for patients. """

	patient_id = models.OneToOneField('auth.user', db_column = 'patient_id', primary_key = True)
	patient_name = models.CharField(max_length = 8)
	gender = models.CharField(max_length = 1, choices = GENDER_CHOICES)
	birth_date = models.DateField()
	marital_status = models.CharField(max_length = 1, choices = MARITAL_CHOICES)
	address = models.CharField(max_length = 20)
	# Maybe working environment should be included.

	class Meta:
		db_table = 'patient_info'
	def __unicode__(self):
		return unicode(self.patient_name)


class Doctor_Info(models.Model):
	""" Position Infomation for doctors. """
	doctor_id = models.OneToOneField('auth.user', db_column = 'doctor_id', primary_key = True)
	doctor_name = models.CharField(max_length = 8, db_index = True)
	gender = models.CharField(max_length = 1, choices = GENDER_CHOICES)
	department = models.ForeignKey('Department')
	phone_num = models.CharField(max_length = 14)
	address = models.CharField(max_length = 20)
	class Meta:
		db_table = 'doctor_info'
	def __unicode__(self):
		return unicode(self.doctor_name)

class Medicine_Info(models.Model):
	""" Information for Medicine.
		In accordance with http://drug.health.sohu.com and
		http://www.en8848.com.cn/yingyu/36/n-117036.html
		Sample data from: http://health.sohu.com/health/drugsearch.htm
	"""
	medicine_id = models.AutoField(primary_key = True)
	brad_name = models.CharField('Brand name', max_length = 10, db_index = True)
	gnrc_name = models.CharField('Generic name', max_length = 30)
	composition = models.TextField()
	is_ota = models.BooleanField('OTA or Rx', choices = OTA_RX_CHOICES)
	indication = models.TextField()
	contraindication = models.TextField()
	administration = models.TextField()
	side_effect = models.TextField()
	producer = models.CharField(max_length = 30)
	class Meta:
		db_table = 'medicine_info'
	def __unicode__(self):
		return unicode(self.brad_name)

class Medicine_Inventory(models.Model):
	""" 单位、数量、到货日期、品名、剂型、规格、批准文号、批号、生产厂商、有效期、质量状况、验收结论和验收人员 """
	medicine_id = models.ForeignKey('Medicine_Info')
	unit = models.CharField(max_length = 4, choices = UNITS)
	ammount = models.DecimalField(max_digits = 10, decimal_places = 2)
	date_in = models.DateField(auto_now = True)
	shelf_life_year = models.IntegerField()
	shelf_life_month = models.IntegerField()
	shelf_life_day = models.IntegerField()
	person_in_charge = models.ForeignKey('auth.User')
	class Meta:
		db_table = 'medicine_inventory'

class Appointment(models.Model):
	""" Appointment between patients and doctors. """
	patient_id = models.ForeignKey('Patient_Info')
	doctor_id = models.ForeignKey('Doctor_Info')
	date_time = models.DateTimeField(auto_now_add = False)
	memo = models.TextField()
	class Meta:
		db_table = 'appointment'
	def __unicode__(self):
		return unicode(self.date_time)

class Diagnosis(models.Model):
	""" One Diagnosis between Doctor and patient. """
	diagnosis_id = models.AutoField(primary_key = True)
	patient_id = models.ForeignKey('Patient_Info')
	doctor_id = models.ForeignKey('Doctor_Info')
	description = models.TextField()
	date_time = models.DateTimeField(auto_now = True, auto_now_add = True)
	class Meta:
		db_table = 'diagnosis'

class Prescription(models.Model):
	""" Prescription for one diagnosis. """
	diagnosis_id = models.ForeignKey('Diagnosis')
	medicine_id = models.ForeignKey('Medicine_Info')
	unit = models.CharField(max_length = 4, choices = UNITS)
	amount = models.DecimalField(max_digits = 10, decimal_places = 2)
	class Meta:
		db_table = 'prescription'